##*****************************************************************************
## TABLE 1
##*****************************************************************************
## 
## R Version: R version 3.2.4 (2016-03-1)
##


##LOADING PACKAGES
library(texreg) ##Version: 1.36.4

##SET WORKING DIRECTORY
setwd('/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
cert.data <- read.csv('replication_materials/data/cert_petition_data_matched_to_dime.csv'


##REGRESSION MODELS

##MODEL 1
m1 <- (glm(I(ddirect)~cfscore_pet ,data=cert.data,subset=decisionDirection!=3 ,family=binomial()))
tt1 <- table(predict(m1)>0,m1$y);sum(diag(tt1))/sum(tt1)

##MODEL 2
m2 <- (glm(I(ddirect)~cfscore_pet + cfscore_resp,data=cert.data,subset=decisionDirection!=3,family=binomial())) 
tt2 <- table(predict(m2)>0,m2$y);sum(diag(tt2))/sum(tt2)

##MODEL 3
m3 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp),data=cert.data,subset=decisionDirection!=3,family=binomial()))
tt3 <- table(predict(m3)>0,m3$y);sum(diag(tt3))/sum(tt3)

##MODEL 4
m4 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp) + factor(issueAreaName),data=cert.data,subset=decisionDirection!=3,family=binomial()))
tt4 <- table(predict(m4)>0,m4$y);sum(diag(tt4))/sum(tt4) 


##WRITING TABLE TO TEX FILE
texreg(list(m1,m2,m3,m4),omit='issueArea',
       stars = c(0),
       custom.coef.names=c('(Intercept)',
           'DIME score of Petitioning Atty.',
           'DIME score of Respondent Atty.',
           '(DIME score of Petitioning Atty. $-$ DIME score of Respondent Atty.)',
           names(m4$coefficients)[-c(1:2)]),
       file='replication_materials/tables/table_1_voting_with_judges.tex')

